package com.pat.java.base.io;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class ReadClient {
	public static void main(String args[]) {
		try {

			Socket clientSocket = new Socket("www.xjtu.edu.cn", 80);
			System.out.println("Client1: " + clientSocket);

			DataOutputStream outbound = new DataOutputStream(
					clientSocket.getOutputStream());
			DataInputStream inbound = new DataInputStream(
					clientSocket.getInputStream());
			InputStreamReader inS = new InputStreamReader(inbound);
			File f = new File("files/xjtu.html");

			FileOutputStream fOut = new FileOutputStream(f);
			PrintStream p = new PrintStream(fOut);
			outbound.writeBytes("GET / HTTP/1.0\r\n\r\n");

			int c;
			while ((c = inS.read()) != -1)
				p.print((char) c);

			inS.close();
			outbound.close();
			inbound.close();
			clientSocket.close();

		} catch (UnknownHostException uhe) {
			System.out.println("UnknownHostException: " + uhe);
		} catch (IOException ioe) {
			System.err.println("IOException: " + ioe);
		}
	}
}
